package org.jackysoft.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jackysoft.annotations.ActionRequestMethod;
import org.jackysoft.annotations.MethodInceptor;
import org.springframework.web.servlet.ModelAndView;

@MethodInceptor(method = { ActionRequestMethod.get, ActionRequestMethod.getone,
		ActionRequestMethod.findone })
public class GetInterceptor extends AbstractInterceptor {

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		Method[] beanMethods = bean.getClass().getMethods();
		Method getId = null;
		for (Method m : beanMethods) {
			if (m.isAnnotationPresent(org.jackysoft.annotations.Id.class)) {
				getId = m;
				break;
			}
		}

		if (getId == null)
			throw new RuntimeException(
					"请在 VO 使用 org.jackysoft.annotations.BID 标注业务主键字段!");

		Object id = getId.invoke(bean);

		Method serviceGetBean = service.getClass().getMethod("get",
				java.io.Serializable.class);

		// 从数据库查询得到的对象

		Object target = serviceGetBean.invoke(service, id);

		modelAndView.addObject("bean", target);

	}

}
